<template>
  <a-modal
    title="商品导出"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
    style="top:20px;"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="选择店铺" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-checkbox v-model="storeValue" :options="storeOptions" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { downFile, httpAction } from '@/api/manage'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import { getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JCheckbox from '@/components/jeecg/JCheckbox'

  export default {
    name: "PmsProductExportModal",
    components: {
      JCheckbox,
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        headers:{},
        storeOptions:[],
        storeValue:'',
        form: this.$form.createForm(this),
        url: {
          queryList: '/stm/stmStoreInfo/queryList',
          exportXlsUrl: 'pms/pmsProduct/exportXls',
        },
      }
    },
    created () {

    },
    methods: {
      show(record) {
        this.form.resetFields();
        this.visible = true;
        this.storeValue = '';
        this.getStore();
        this.model = record;
      },
      getStore() {
        this.storeOptions = [];
        getAction(this.url.queryList).then(res=>{
          if(res.success){
            console.log('StoreData => ' + res.result);
            res.result.forEach(item => {
              this.storeOptions.push({ value: item.storeId.toString(), label: item.name })
            })
          }else{
            console.log(res.message);
          }
        });
      },
      close () {
        this.form.resetFields();
        this.$emit('close');
        this.visible = false;
      },
      handleSubmit () {
        console.log(this.storeValue)
        let param = {...this.model};
        let fileName = '商品信息';
        if (this.storeValue != '') {
          param['stores'] = this.storeValue
        }
        downFile(this.url.exportXlsUrl,param).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>
<style scoped>

</style>